package com.tms.app.tms.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.tms.app.tms.app.MyApp;
import com.tms.app.tms.bean.GpsBean;
import com.tms.app.tms.bean.UpdateInfo;
import com.tms.app.tms.bean.UserInfo;
import com.tms.app.tms.db.TaskInfoDBUtil;
import com.tms.app.tms.test.db.MyMessage;
import com.tms.app.tms.test.service.AlarmReceiver;
import com.tms.app.tms.ui.base.presenter.BaseListPresenter;
import com.tms.app.tms.utils.LocationUtil;
import com.tms.app.tms.utils.SharePreferenceUtil;
import com.tms.app.tms.utils.TimeUtil;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.edu.zafu.camera.HeadChar;

import static com.tms.app.tms.utils.TimeUtil.DEFAULT_DATE_FORMAT2;

/**
 * Created by Administrator on 2017/11/20 0020.
 */

public class UploadGPS extends Service implements AMapLocationListener {
    private AlarmManager am;
    private PendingIntent pi;
    private AMapLocationClient mapLocationClient;
    BaseListPresenter presenter=new BaseListPresenter();
    private String token;
    private double lat=0;
    private double lon=0;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        token= SharePreferenceUtil.getToken(this);
        mapLocationClient = LocationUtil.getLocationClient(this);
        mapLocationClient.setLocationOption(LocationUtil.initDefaultOption());
        mapLocationClient.setLocationListener(this);
        mapLocationClient.startLocation();

    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mapLocationClient = LocationUtil.getLocationClient(this);
        mapLocationClient.setLocationOption(LocationUtil.initDefaultOption());
        mapLocationClient.setLocationListener(this);
        mapLocationClient.startLocation();
        gpsUplaod();
        return START_REDELIVER_INTENT; //这里为了提高优先级，选择START_REDELIVER_INTENT 没那么容易被内存清理时杀死
    }


    private void gpsUplaod(){
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                if (TaskInfoDBUtil.checkTaskdoing()){
//                    if (lat>0&&lon>0){
//                        saveLocalGps(lat,lon);
//                    }
                    presenter.uploadGps(token);
                }
            }
        };
        TimerTask task2 = new TimerTask() {
            @Override
            public void run() {
                if (TaskInfoDBUtil.checkTaskdoing()){
                    if (lat>0&&lon>0){
                        saveLocalGps(lat,lon);
                    }
                    //presenter.uploadGps(token);
                }
            }
        };

        Timer timer = new Timer();
        timer.schedule(task,1000,1000*60*6);
        Timer timer2 = new Timer();
        timer2.schedule(task2,500,1000*60);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapLocationClient.onDestroy();
        mapLocationClient=null;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        System.out.print("GPS："+aMapLocation.getLatitude());
      //  Toast.makeText(this, "服务定位成功", 1000).show();
        lat=aMapLocation.getLatitude();
        lon=aMapLocation.getLongitude();
      //  saveLocalGps(lat,lon);

    }

    public void saveLocalGps(Double lat,Double lon) {
        int planId = SharePreferenceUtil.getInt(this,"planId",0);
        UserInfo userInfo=SharePreferenceUtil.getObject(this, UserInfo.class);
//        if (userInfo!=null
//                &&userInfo.getId()>0&&userInfo.getHeadstockId()>0
//                &&userInfo.getTrailerId()>0&&planId>0){
            if (userInfo!=null
                    &&userInfo.getId()>0&&planId>0){
            GpsBean gpsBean=new GpsBean();
            gpsBean.setLat(lat+"");
            gpsBean.setLon(lon+"");
            gpsBean.setPlanId(planId);
            gpsBean.setDriverId(userInfo.getId());
            gpsBean.setCarId(userInfo.getHeadstockId());
            gpsBean.setTrailerId(userInfo.getTrailerId());
            long time= Long.parseLong(TimeUtil.getTime(new Date(),DEFAULT_DATE_FORMAT2));
            gpsBean.setTime(time);
            try {
                DbManager   db = x.getDb(MyApp.getDaoConfig());
                if (db == null)
                    return;
                db.save(gpsBean);
            } catch (DbException e) {
                e.printStackTrace();
            }
        }
    }
}